from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny, IsAuthenticated
from apiapp import serializers
from rest_framework.authtoken.models import Token
from drf_spectacular.utils import extend_schema, OpenApiExample, OpenApiResponse
from drf_spectacular.types import OpenApiTypes
from apiapp import responses
from apiapp.permissions import IsAdmin


# Create your views here.


class UserRegisterView(APIView):
    permission_classes = [AllowAny, ]

    @extend_schema(
        request=serializers.UserRegisterSerializer,
        responses={
            status.HTTP_200_OK: responses.TokenResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'token': 'generated-token'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def post(self, request):
        serializer = serializers.UserRegisterSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        token, _ = Token.objects.get_or_create(user=user)
        return Response({
            'success': True,
            'token': token.key,
            'isAdmin': user.isAdmin
        })


class UserLoginView(APIView):
    permission_classes = [AllowAny, ]

    @extend_schema(
        request=serializers.UserLoginSerializer,
        responses={
            status.HTTP_200_OK: responses.TokenResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'token': 'generated-token'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def post(self, request):
        serializer = serializers.UserLoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        token, _ = Token.objects.get_or_create(user=user)
        return Response({
            'success': True,
            'token': token.key,
            'isAdmin': user.isAdmin
        })


class TourView(APIView):
    def get_permissions(self):
        if self.request.method == 'GET':
            return [AllowAny()]
        return [IsAuthenticated()]

    @extend_schema(
        request=serializers.AddTourSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Тур успешно создан'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def post(self, request):
        serializer = serializers.AddTourSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save()
        return Response(response)

    @extend_schema(
        request=serializers.GetAllToursSerializer,
        responses={
            status.HTTP_200_OK: responses.TourResponse(many=True)
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={
                    'id': 1,
                    'name': "Название тура",
                    'description': "Описание тура",
                    'price': 1200,
                    'seats': 20,
                    'isActive': True,
                    'image': 'base64_image'
                },
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def get(self, request):
        serializer = serializers.GetAllToursSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)



class TourDetailView(APIView):
    def get_permissions(self):
        if self.request.method == 'GET':
            return [AllowAny()]
        return [IsAdmin()]

    @extend_schema(
        request=serializers.GetTourSerializer,
        responses={
            status.HTTP_200_OK: responses.TourResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={
                    'id': 1,
                    'name': "Название тура",
                    'description': "Описание тура",
                    'price': 1200,
                    'seats': 20,
                    'isActive': True,
                    'image': 'base64_image',
                },
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def get(self, request, id):
        serializer = serializers.GetTourSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id, user=request.user)
        return Response(response)


    @extend_schema(
        request=serializers.TourDeleteSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Tour Deleted'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def delete(self, request, id):
        serializer = serializers.TourDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id)
        return Response(response)

    @extend_schema(
        request=serializers.TourEditSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Tour Edited'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def patch(self, request, id):
        serializer = serializers.TourEditSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id)
        return Response(response)

class UserView(APIView):
    permission_classes = [IsAuthenticated, ]

    @extend_schema(
        responses={
            status.HTTP_200_OK: responses.UserResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={
                    'id': 1,
                    'first_name': "Имя",
                    'last_name': "Фамилия",
                    'email': "email@example.com",
                    'isAdmin': True,
                },
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def get(self, request):
        serializer = serializers.GetUserInfoSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)


class TourDescriptionView(APIView):
    permission_classes = [IsAdmin,]

    @extend_schema(
        request=serializers.TourDescriptionGenerator,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Сгенерированное описание'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def post(self, request):
        serializer = serializers.TourDescriptionGenerator(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save()
        return Response(response)



class TourBookView(APIView):
    permission_classes = [IsAuthenticated,]

    @extend_schema(
        request=serializers.TourAddBookSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Тур забронирован'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def post(self, request, id):
        serializer = serializers.TourAddBookSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id, user=request.user)
        return Response(response)


    @extend_schema(
        request=serializers.TourRemoveBookSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={'success': True, 'message': 'Бронь с тура убрана'},
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def delete(self, request, id):
        serializer = serializers.TourRemoveBookSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id, user=request.user)
        return Response(response)


class AllUserToursView(APIView):
    permission_classes = [IsAuthenticated,]

    @extend_schema(
        request=serializers.AllUserToursSerializer,
        responses={
            status.HTTP_200_OK: responses.UserTourResponse(many=True)
        },
        examples=[
            OpenApiExample(
                'Success Response',
                value={
                    'id': 1,
                    'name': "Название тура",
                    'description': "Описание тура",
                    'price': 1200,
                    'seats': 20,
                    'isActive': True,
                    'image': 'base64_image',
                    'isPaid': False,
                },
                status_codes=[200],
                response_only=True,
            )
        ]
    )
    def get(self, request):
        serializer = serializers.AllUserToursSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)


class YooMoneyPaidView(APIView):
    permission_classes = [AllowAny]

    @extend_schema(
        request=serializers.AllUserInToursSerializer,
        responses={
            status.HTTP_200_OK: responses.MessageResponse
        },
    )
    def post(self, request):
        serializer = serializers.YooMooneyPaidSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save()
        return Response(response)


class TourUsersView(APIView):
    permission_classes = [IsAdmin, ]

    @extend_schema(
        request=serializers.AllUserInToursSerializer,
        responses={
            status.HTTP_200_OK: responses.AllUsersResponse(many=True)
        },
    )
    def get(self, request, id):
        serializer = serializers.AllUserInToursSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(id=id)
        return Response(response)
